import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
			return {
				//数据
        searchValue: '',
				thatUrl: this.urls.baseUrl,
				FileUpload: [],
				testAttachment: '', // 文本附件

				imageAttachment: '',
				FileUploadImg: [],

				videoAttachment: '',
				FileUploadVideo: [],
				id: '',
				validate: {},
				obj: {},
				enterprise: [], // table
				total: 0, //总条数
				page: 1, //当前页
				size: 10, //每页显示条数
				ApplicationStatus: false,
				Application: false,
				curId: 0,
				uploadNum: 0,
				isModify: false, //true 修改  false新增
			}
		},
		components: {
			pageination
		},
		created() { //调用方法
			this.getData();
		},
		mounted() {
			jQuery.validator.addMethod("isMobile", function(value, element) {
				let length = value.length;
				let mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
				return this.optional(element) || (length == 11 && mobile.test(value));
			}, "请正确填写手机号码");
			//加载表单验证
			this.validate = $("#enterpriseForm").validate({
				ignore: [],
				rules: {
					name: { // 名称
						required: true,
						maxlength: 50
					},
					address: { // 名称
						required: true,
						maxlength: 50
					},
					reserved: { // 名称
						required: true,
						maxlength: 10
					},
					contact: { // 名称
						required: true,
						isMobile: true
					},
					financial: { // 名称
						required: true,
						maxlength: 10
					},
					financialPhone: { // 名称
						required: true,
						isMobile: true
					},
					person: {
						maxlength: 10
					},
					money: {
						number: true
					}

				},
				messages: {
					name: { // 名称
						required: '企业名称不能为空!',
						maxlength: '最大长度为50!'
					},
					address: { // 名称
						required: '地址不能为空!',
						maxlength: '最大长度为50!'
					},
					reserved: { // 名称
						required: '联系人不能为空!',
						maxlength: '最大长度为10!'
					},
					contact: { // 名称
						required: '联系方式不能为空!',
						isMobile: '请输入正确的联系方式!'
					},
					financial: { // 名称
						required: '财务联系人不能为空!',
						maxlength: '最大长度为50!'
					},
					financialPhone: { // 名称
						required: '财务联系方式不能为空!',
						isMobile: '请输入正确的联系方式!'
					},
					person: {
						maxlength: '最大长度为10'
					},
					money: {
						number: '注册资金只能为数字'
					}
				},
				/*  errorElement: "em",
				  errorPlacement: function(error, element) {
				    error.appendTo(element.parents(".error-box").children(".error-msg"));
				  }*/
			});
		},
		methods: { //写函数方法
			getData: function() {
				let that = this;
				let obj = {
					"pageNum": that.page,
					"pageSize": that.size
				};
				let name = $('#searchName').val();
				let value = $('#searchValue').val();
				obj[name] = value;
				that.$post(
						that.urls.getEnterprisePaging,
						obj
					)
					.then(function(res) {
						if (res.success) {
							that.enterprise = res.pageInfo.list;
							that.page = res.pageInfo.pageNum;
							that.total = res.pageInfo.total;

						}
					})
			},

			// 提交
			subForm: function() {
				let that = this;
				let flg = that.validate.form();
				if (flg) {
					let dataObj = that.combinationFormObject(that.validate.currentForm);
					dataObj.auditStatus = 1;
					let url;
					let msg;
					if (that.id) {
						url = that.urls.editEnterprise;
						msg = '修改企业成功！';
						dataObj.id = that.id;
					} else {
						url = that.urls.addEnterprise;
						msg = '新增企业成功成功！';
					}
					that.$post(
							url,
							dataObj
						)
						.then(function(res) {

							if (res.success) {
								that.ApplicationStatus = false;
								layer.msg(msg, {
									time: 3000, //3s后自动关闭
								});
								that.testAttachment = '';
								that.FileUpload = [];
								that.getData();
							} else {
								layer.msg('失败！', {
									time: 3000, //3s后自动关闭
								});
							}
						})
				}
			},

			//页码切换执行方法
			pageFn: function(val, pid) {
				this.page = parseInt(val);
				this.getData();
				//此处调列表数据接口
			},
			/**返回子组件用户选择的一页显示条数**/
			obtainChild: function(data) {
				this.row = data;
			},
			//人工录入及修改模态框
			manually: function(id) {
        $("#enterpriseForm").validate().resetForm();
        $("input[type='reset']").trigger("click");

			  let that = this;
				that.curId = 0;
				that.id = 0;
				that.clearAll();
				this.ApplicationStatus = true;
				if (id < 1) {
					this.isModify = false;
					return
					//  that.id = id;
				}
				this.isModify=true;
				that.id = id;
				that.$post(
						that.urls.getEnterprise, {
							"id": id
						}
					)
					.then(function(res) {
						if (res.success) {
							that.obj = res.data;

							that.testAttachment = res.data.testAttachment;
							that.imageAttachment = res.data.imageAttachment;
							that.videoAttachment = res.data.videoAttachment;

							let enclosureList = []; // 查看tex

							let enclosures = res.data.testAttachment.split(";");
							if (enclosures.length > 0 && res.data.testAttachment) {
								enclosures.forEach(function(e) {
									enclosureList.push({
										"name": that.getFileName(that.getFileName(e)),
										"url": that.urls.baseUrl + e
									});
								})
							}
							that.FileUpload = enclosureList;

							let imgArr = [];
							// 查看img
							let enclosuresImg = res.data.imageAttachment.split(";");
							if (enclosuresImg.length > 0 && res.data.imageAttachment) {
								enclosuresImg.forEach(function(e) {
									imgArr.push({
										"name": that.getFileName(that.getFileName(e)),
										"url": that.urls.baseUrl + e
									});
								})
							}
							that.FileUploadImg = imgArr;

							let video = []; // 查看video
							let enclosuresVideo = res.data.videoAttachment.split(";");
							if (enclosuresVideo.length > 0 && res.data.videoAttachment) {
								enclosuresVideo.forEach(function(e) {
									video.push({
										"name": that.getFileName(that.getFileName(e)),
										"url": that.urls.baseUrl + e
									});
								})
							}
							that.FileUploadVideo = video;

						}
					})
			},
			//查看
			toView: function(id) {
				let that = this;
				that.clearAll();
				that.curId = 0;

				that.$post(
						that.urls.getEnterprise, {
							"id": id
						}
					)
					.then(function(res) {
						if (res.success) {
							that.obj = res.data;

							that.testAttachment = res.data.testAttachment;
							that.imageAttachment = res.data.imageAttachment;
							that.videoAttachment = res.data.videoAttachment;

							let enclosureList = []; // 查看tex

							let enclosures = res.data.testAttachment.split(";");
							if (enclosures.length > 0 && res.data.testAttachment) {
								enclosures.forEach(function(e) {
									enclosureList.push({
										"name": that.getFileName(that.getFileName(e)),
										"url": that.urls.baseUrl + e
									});
								})
							}
							that.FileUpload = enclosureList;

							let imgArr = [];
							// 查看img
							let enclosuresImg = res.data.imageAttachment.split(";");
							if (enclosuresImg.length > 0 && res.data.imageAttachment) {
								enclosuresImg.forEach(function(e) {
									imgArr.push({
										"name": that.getFileName(that.getFileName(e)),
										"url": that.urls.baseUrl + e
									});
								})
							}
							that.FileUploadImg = imgArr;

							let video = []; // 查看video
							let enclosuresVideo = res.data.videoAttachment.split(";");
							if (enclosuresVideo.length > 0 && res.data.videoAttachment) {
								enclosuresVideo.forEach(function(e) {
									video.push({
										"name": that.getFileName(that.getFileName(e)),
										"url": that.urls.baseUrl + e
									});
								})
							}
							that.FileUploadVideo = video;
							console.log(that.FileUpload)
							console.log(that.FileUploadImg)
							console.log(that.FileUploadVideo)
						}
					})

				this.Application = true;
			},
			//关闭模态框
			closeModel: function() {
				this.Application = false;
				this.ApplicationStatus = false;
			},

			touchOffIdInput(n) {
				this.uploadNum = n;
				if (n == 1) {
					$("#imageAttachment1").click();
				}
			},
			/**文件上传(多文件)js  文本附件 **/
			idFrontImg: function(event) {
				let that = this;
				let files = event.currentTarget.files;
				if (!that.fileGlobalFilter(files)) {
					return;
				}
				let formData = new FormData();
				for (let i = 0, len = files.length; i < len; i++) {
					formData.append('fileNames', files[i]);
				}
				let config = {
					headers: {
						'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
					}
				};
				that.$post(
						that.urls.upload,
						formData,
						config
					)
					.then(function(res) {
						if (res.success) {
							let fileInfos = res.data;
							let urls = [];
							for (let i = 0, len = fileInfos.length; i < len; i++) {
								urls.push(fileInfos[i].url);
								that.FileUpload.push({
									"name": that.getFileName(fileInfos[i].url),
									"url": fileInfos[i].url
								});
							}
							console.log(urls)
							if (that.testAttachment.length > 0) {
								that.testAttachment += ";" + urls.join(";");
							} else {
								that.testAttachment = urls.join(";");
							}

						}
					})
			}, // 文本点击点击

			touchImg(n) {
				this.uploadNum = n;
				if (n == 1) {
					$("#touchImg").click();
				}
			}, // 图片

			touchUploadImg: function(event) {
				let that = this;
				let files = event.currentTarget.files;
				if (!that.fileGlobalFilter(files)) {
					return;
				}
				let formData = new FormData();
				for (let i = 0, len = files.length; i < len; i++) {
					formData.append('fileNames', files[i]);
				}
				let config = {
					headers: {
						'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
					}
				};
				that.$post(
						that.urls.upload,
						formData,
						config
					)
					.then(function(res) {
						if (res.success) {
							let fileInfos = res.data;
							let urls = [];
							for (let i = 0, len = fileInfos.length; i < len; i++) {
								urls.push(fileInfos[i].url);
								that.FileUploadImg.push({
									"name": that.getFileName(fileInfos[i].url),
									"url": fileInfos[i].url
								});
							}
							console.log(urls)
							if (that.imageAttachment.length > 0) {
								that.imageAttachment += ";" + urls.join(";");
							} else {
								that.imageAttachment = urls.join(";");
							}

						}
					})
			}, // 图片点击

			touchVideo(n) {
				this.uploadNum = n;
				if (n == 1) {
					$("#touchVideo").click();
				}
			}, // 视频

			idFrontVideo: function(event) {
				let that = this;
				let files = event.currentTarget.files;
				if (!that.fileGlobalFilter(files)) {
					return;
				}
				let formData = new FormData();
				for (let i = 0, len = files.length; i < len; i++) {
					formData.append('fileNames', files[i]);
				}
				let config = {
					headers: {
						'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
					}
				};
				that.$post(
						that.urls.upload,
						formData,
						config
					)
					.then(function(res) {
						if (res.success) {
							let fileInfos = res.data;
							let urls = [];
							for (let i = 0, len = fileInfos.length; i < len; i++) {
								urls.push(fileInfos[i].url);
								that.FileUploadVideo.push({
									"name": that.getFileName(fileInfos[i].url),
									"url": fileInfos[i].url
								});
							}
							console.log(urls)
							if (that.videoAttachment.length > 0) {
								that.videoAttachment += ";" + urls.join(";");
							} else {
								that.videoAttachment = urls.join(";");
							}

						}
					})
			}, // 视频点击

			clearAll: function() {
				this.obj = {};
				this.testAttachment = '';
				this.FileUpload = [];
				this.imageAttachment = '';
				this.FileUploadImg = [];
				this.videoAttachment = '';
				this.FileUploadVideo = [];
			},

			downloadResources: function(filePath) {
				if (filePath != null && filePath != "") {
					window.location.href = this.urls.download + "?path=" + filePath;
				} else {
					layer.msg('暂无附件', {
						time: 3000, //3s后自动关闭
					});
				}
			},

			// 跳转到预览
			toSrc: function(url) {
				console.log(this.thatUrl + url)
				window.open(this.thatUrl + url);
			},

			// 点击切换按钮
			clickcurId: function(id) {
				this.curId = id;
				console.log(this.curId)
			},

			//删除附件
			del: function(f) {
				let that = this;

				layer.confirm('你确定要删除吗?', {
					icon: 3,
					title: '提示'
				}, function(index) {

					if (that.curId == 0) {
						if (that.FileUpload.length > 0) {
							let arr = [];
							let urls = [];
							that.FileUpload.forEach(function(e) {
								if (f.name != e.name) {
									urls.push(e.url);
									arr.push(e);
								}
							})
							that.FileUpload = arr;
							that.testAttachment = urls.join(";");
						}
					} else if (that.curId == 1) {
						if (that.FileUploadImg.length > 0) {
							let arr = [];
							let urls = [];
							that.FileUploadImg.forEach(function(e) {
								if (f.name != e.name) {
									urls.push(e.url);
									arr.push(e);
								}
							})
							that.FileUploadImg = arr;
							that.imageAttachment = urls.join(";");
						}
					} else if (that.curId == 2) {
						if (that.FileUploadVideo.length > 0) {
							let arr = [];
							let urls = [];
							that.FileUploadVideo.forEach(function(e) {
								if (f.name != e.name) {
									urls.push(e.url);
									arr.push(e);
								}
							})
							that.FileUploadVideo = arr;
							that.videoAttachment = urls.join(";");
						}
					}

				});

			},

			subStrThe: function(str) {
				if (str) {
					if (str.length > 10) {
						let s = str.substr(0, 9);
						return s + '...';
					} else {
						return str;
					}
				}
			},
    // 导出
      exportEnterprise: function () {
          window.location.href = this.urls.exportEnterprise + "?"  + $('#searchName').val() + "=" + this.searchValue;
      },

      //导入数据
      inputData: function (event) {
        let that = this;
        var file = event.currentTarget.files[0];
        if (!that.fileGlobalFilter(file)) {
          return;
        }
        let formData = new FormData();
        formData.append('fileNames', file);
        let config = {
          headers: {
            'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
          }
        };
        that.$post(
          that.urls.enterpriseImportExcel,
          formData,
          config
        )
          .then(function(res) {
            let msg = '';
            if(res.success) {
              msg = "导入成功！";
              that.getData();
            } else {
              msg = "导入失败！";
            }
            layer.msg(msg, {
              time: 3000, //3s后自动关闭
            });
          })
      }


		}
}
